package com.okaysoft.ofb

import org.springframework.dao.DataIntegrityViolationException
import grails.converters.JSON

class FormworkController {

    static allowedMethods = [save: "POST", update: "POST"]
    
    def formworkService

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {

    }

    def listJson() {
        params.max = Math.min(params.rows ? params.int('rows') : 10, 100)
        params.page = params.page ? params.int('page') : 0
        params.offset = (params.page - 1) * params.max

        render(formworkService.listJson(params))
    }

    def treeJson() {
        def ft = formworkService.formworkTreeJson(null) as JSON
        render ft.toString()
    }
    
    def showJson() {
        if (!params.id) {
            render('')
            return
        }
        def f = Formwork.get(params.id)
        if (!f) {
            render('')
            return
        }
        def ft = formworkService.formworkCompositionData(f) as JSON
        render ft.toString()
    }

    def categoryTreeJson() {
         def categoryTree = formworkService.formworkCategoryTreeJson(null) as JSON
        render categoryTree.toString()
    }

    def save() {
        if (!params.name) {
            def failure = [status: 0,msg:'模板名称不能为空'] as JSON
            render(failure.toString())
            return
        }
        try {
            formworkService.save(params)
        } catch (RuntimeException e) {
            log.error(e.getMessage())
            def failure = [status: 0,msg:e.message] as JSON
            render(failure.toString())
            return
        }
        
        def succ = [status: 1,msg:message(code: 'default.save.success.message', default: '保存成功')] as JSON
        render(succ.toString())
    }

    def show() {
        def formworkInstance = Formwork.get(params.id)
        if (!formworkInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'formwork.label', default: 'Formwork'), params.id])
            redirect(action: "list")
            return
        }

        [formworkInstance: formworkInstance]
    }

    def delete() {
        def formworkInstance = Formwork.get(params.id)
        if (!formworkInstance) {
            def notfound = [status: 0, msg:  message(code: 'default.not.found.message', args: [message(code: 'formwork.label', default: 'Formwork'), params.id])] as JSON
            render(notfound.toString())
            return
        }

        try {
            formworkInstance.delete(flush: true)
            def succ = [status: 1, msg:  message(code: 'default.deleted.message', args: [message(code: 'formwork.label', default: 'Formwork'), params.id])] as JSON
            render(succ.toString())

        }
        catch (DataIntegrityViolationException e) {
            def failure = [status: 0, msg:  message(code: 'default.not.deleted.message', args: [message(code: 'formwork.label', default: 'Formwork'), params.id])] as JSON
            render(failure.toString())

        }
    }
    
    def saveCategory() {
        def formworkCategory
        if (params.id) {
            formworkCategory = FormworkCategory.get(params.id)
        }
        if(!formworkCategory) {
            formworkCategory = new FormworkCategory()
        }
        formworkCategory.properties = params

        if (!formworkCategory.save(flush: true)) {
            def failure = [status: 0,msg:message(code: 'default.save.failure.message', default: 'Save failure')] as JSON
            render(failure.toString())
            return
        }

        def succ = [status: 1,msg:message(code: 'default.save.success.message', default: 'Save success')] as JSON
        render(succ.toString())
    }

    def categoryDelete() {
        def formworkCategoryInstance = FormworkCategory.get(params.id)
        if (!formworkCategoryInstance) {
            def notfound = [status: 0, msg:  message(code: 'default.not.found.message', args: [message(code: 'formworkCategory.label', default: 'FormworkCategory'), params.id])] as JSON
            render(notfound.toString())
            return
        }

        try {
            formworkCategoryInstance.delete(flush: true)
            def succ = [status: 1, msg:  message(code: 'default.deleted.message', args: [message(code: 'formworkCategory.label', default: 'FormworkCategory'), params.id])] as JSON
            render(succ.toString())

        }
        catch (DataIntegrityViolationException e) {
            def failure = [status: 0, msg:  message(code: 'default.not.deleted.message', args: [message(code: 'formworkCategory.label', default: 'FormworkCategory'), params.id])] as JSON
            render(failure.toString())

        }
    }

    def composition() {
        def compositions = FormworkDetail.compositionMap()
        [compositions:compositions]
    }
    
    def getComposition() {
        def name = params.name
        String content = ''

        try {
            content = g.render(template:"/composition/${name}", model:[])
        } catch (RuntimeException e) {
        }

        render(content)
    }
}
